import {
  upload
} from '../../utils/requestUtils'; 
Component({
  properties: {
    disabled:{
      type:Boolean,
      value:false
    },
    camera:{
      type:Array,
      value:[]
    },
    accept:{
      type:String,
      value:"image"
    },
    title:{
      type:String,
      value:"文件上传"
    },
    watermark:{
      type:String,
      value:""
    },
    maxCount:{
      type:Number,
      value:1
    },
    initList:{
      type:Array,
      value:[]
    },
    businessid: {
      type: String
    },
    tmp_id: {
        type: String
    },
    name: {
        type: String
    },
    businesstype: {
        type: String
    },
    readonly:{
        type:Boolean
    }
  },
  data: {
    fileList: [],
  },
  methods: {
    //上传完成
    async afterRead(event) {
        var that = this
        const { file } = event.detail
        const result =  await upload(file,this.properties.watermark);
        const data =  JSON.parse(result);
        const { fileList = [] } = that.data;
        fileList.push({ ...file, url: data.url,isImageFile:true})
        that.setData({ fileList })
        console.log('s', this.data.fileList)
        that.triggerEvent('uploadHandler',fileList)
      },
      //删除文件（只做前端删除）
      delete(event) {
        var that = this
        const { fileList } = that.data;
        const newFileList = fileList.filter((items) =>{
          return items.url != event.detail.file.url
        })
        that.setData({ fileList : newFileList, })
      },
      //图片初始化
      initListHandler(initList) {
        var that = this
        if(initList && initList.length>0){
          let sourceList = initList.map(v=>{
            return {
                fileType: "image",
                isImageFile:true,
                type:"image",
                url: v.url,
                name:'file'
            }
          })
          that.setData({ fileList : sourceList })
        }else{
          that.setData({ fileList : [] })
        }
      },
      // 返回文件
      returnList(){
        return this.data.fileList
      }
  }
 
})
